package util;

import DAO.MyDrawPane;
import DAO.NodeList;
import DAO.TreeNode;
import javafx.embed.swing.SwingFXUtils;
import javafx.scene.image.WritableImage;
import javafx.stage.FileChooser;
import javafx.stage.Stage;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

/**
 *用于导出为图片文件
 */
public class PhotoUtil {

    //导出后缀名为fileKind的图像文件
    public static void export(String fileKind) {
        // 弹出文件选择器
        FileChooser fileChooser = new FileChooser();
        fileChooser.getExtensionFilters().add(new FileChooser.ExtensionFilter(fileKind+" Files", "*."+fileKind));

        // 设置初始路径为程序根目录
        String currentDirectory = System.getProperty("user.dir");
        fileChooser.setInitialDirectory(new File(currentDirectory));


        Stage primaryStage = new Stage();
        File file = fileChooser.showSaveDialog(primaryStage);
        if (file == null){
            System.out.println("未选中文件");
            return;
        }

        Zoom.recover();
        for (int i = 0; i < NodeList.list.size(); i++) {
            TreeNode node = new TreeNode();
            node = NodeList.list.get(i);
            node.setClick(false);
        }

        MyDrawPane.redraw();
        TreeNode top=getTop();
        TreeNode left=getLeft();

        int x = (int)left.getX()- 50;
        int y = (int)top.getY() - 50;
        // 获取当前绘制的图像
        WritableImage wi = MyDrawPane.g.snapshot(null, null);
        // 计算图像的高度和宽度，加上边距
        double height = wi.getHeight()+ 50*2;
        double width = wi.getWidth()+ 50*2;

        // 裁剪图像以匹配计算出的尺寸和偏移
        WritableImage wi1 = MyDrawPane.drawPane.snapshot(null, null);
        WritableImage wi2 = new WritableImage(wi1.getPixelReader(),x,y,(int)width,(int)height);

        // 将图像转换为BufferedImage 并保存为PNG或JPG文件
        BufferedImage bi = SwingFXUtils.fromFXImage(wi2,null);
        try {
            ImageIO.write(bi,fileKind,file);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //获取顶部的节点
    private static TreeNode getTop() {
        TreeNode top = new TreeNode();
        if(!NodeList.list.isEmpty()) {
            top = NodeList.list.get(0);
            for(int i = 0;i < NodeList.list.size();i++) {
                TreeNode node = new TreeNode();
                node = NodeList.list.get(i);
                //比较
                if(top.getY()-node.getY()>0) {
                    top = node;
                }
            }
        }
        return top;
    }
    //获取左侧的节点
    public static TreeNode getLeft() {
        TreeNode left = new TreeNode();
        if (NodeList.list.size() != 0) {
            left = NodeList.list.get(0);
            for (int i = 0; i < NodeList.list.size(); i++) {
                TreeNode node = new TreeNode();
                node = NodeList.list.get(i);
                //比较
                if (left.getX() - node.getX() > 0) {
                    left = node;
                }
            }
        }
        return left;
    }
}
